<?php
include_once('config/config.php');

$LoginAdmin = auth();

if ($_POST) {
  // 判断action是否存在
  $action = isset($_POST['action']) ? $_POST['action'] : '';


  if($action == 'dep')
  {
    $DepSql = "SELECT * FROM `stu_department`";

    $DepData = all($DepSql);

    if($DepData)
    {
      success('查询成功',$DepData);
    }else{
      error('暂无数据');
    }
  }


  // 封装数据
  $data = [
    'name' => $_POST['name'],
    'age' => $_POST['age'],
    'sex' => $_POST['sex'],
    'depid' => $_POST['depid'],
  ];

 
  $parents_phone = !empty($_POST['parents_phone']) ? $_POST['parents_phone'] : '';

  if(strlen($_POST['parents_phone']) != 11)
  {
    ShowMsg('手机号是11位');
  }

  $data['parents_phone'] = $parents_phone;


  $result = add('student',$data);

  if($result)
  {
    ShowMsg('新增成功','studentlist.php');
  }else{
    ShowMsg('新增失败');
  }


}

?>

<!DOCTYPE html>
<html lang="en">

<head>
  <?php include_once('common/meta.php') ?>
</head>

<!--[if lt IE 7 ]> <body class="ie ie6"> <![endif]-->
<!--[if IE 7 ]> <body class="ie ie7 "> <![endif]-->
<!--[if IE 8 ]> <body class="ie ie8 "> <![endif]-->
<!--[if IE 9 ]> <body class="ie ie9 "> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!-->
<style>
  input[type="file"] {
    display: none;
  }

  label[for="avatar"] {
    width: 200px;
  }
</style>

<body>
  <!--<![endif]-->

  <?php include_once('common/header.php') ?>

  <?php include_once('common/nav.php') ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">新增学生</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li><a href="studentlist.php">StudentList</a> <span class="divider">/</span></li>
      <li class="active">StudentAdd</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='studentlist.php'"><i class="icon-list"></i> 学生列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">

                <label>学生姓名</label>
                <input type="text" name="name" class="input-xxlarge" placeholder="请输入学生姓名">

                <label>年龄</label>
                <input type="number" name="age" class="input-xxlarge" placeholder="请输入年龄">
                <label>家长电话</label>
                <input type="tel" name="parents_phone" class="input-xxlarge" placeholder="请输入家长电话">

                <label>性别</label>
                <select name="sex" class="input-xlarge">
                  <option value="1">男</option>
                  <option value="2">女</option>
                </select>

                <label>所属系部</label>
                <select name="depid" id="depid" class="input-xlarge">
                  <option value="0">请选择</option>
                </select>

                <label></label>
                <input class="btn btn-primary" type="submit" value="提交" />
              </form>
            </div>
          </div>
        </div>

        <?php include_once('common/footer.php') ?>

      </div>
    </div>
  </div>
</body>

</html>
<?php include_once('common/script.php') ?>

<script>

  // 部门
  $.ajax({
    type:'post',
    url:'studentadd.php',
    data:{
      action:'dep'
    },
    dataType:'json',
    success:function(res)
    {
      if(res.code === 1)
      {
        var option = "<option value='0'>请选择</option>"

        for(var item of res.data)
        {
          option += `<option value="${item.id}">${item.name}</option>`
        }

        $('#depid').html(option)
      }
    }
  })

</script>